package com.hxy.platform.admin.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie工具
 */
public class CookieUtil {
    /**
     * 添加信息到cookie中
     *
     * @param value
     * @param response
     */
    public static void addCookie(String key, String value, HttpServletResponse response) {
        // 按照 key value 把信息存入cookie中
        Cookie cookie = new Cookie(key, value);
        // 设置cookie过期时间
        cookie.setMaxAge(5 * 60);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    /**
     * 遍历cookie信息
     */
    public static String getValueByCookie(String key, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(key)) {
                return cookie.getValue();
            }
        }
        return null;
    }
}
